package org.zjx.window.action.impl;

import lombok.SneakyThrows;
import org.zjx.window.action.Action;
import org.zjx.window.operation.WindowOperation;
import org.zjx.window.window.IWindow;
import org.zjx.window.window.impl.OpenWindow;
import org.zjx.window.window.impl.StartWindow;

import java.awt.*;
import java.io.File;
import java.io.IOException;

public class OpenGameAction extends Action {

    public OpenGameAction(IWindow targetWindow) {
        super(targetWindow, true);
    }

    @Override
    protected void doExecute() {
        boolean open = false;
        while (StartWindow.getWindows().isEmpty()) {
            if (!open) openWindow();
            targetWindow.execute(WindowOperation.delay(1000));
        }
        ((OpenWindow)targetWindow).setExists(false);
    }

    @SneakyThrows
    public void openWindow() {
        String gameExePath = ((OpenWindow) targetWindow).getGameExePath();
        File gameFile = new File(gameExePath);
        if (!gameFile.exists()) {
            throw new IOException("游戏可执行文件不存在: " + gameExePath);
        }

        Desktop.getDesktop().open(gameFile);
    }
}
